package com.fallsvc.dayproblem.demo202511;

import java.util.*;

/**
 * @auther falls_vc
 * description:
 * @date 2024/11/17 10:58
 */
public class Demo20251117 {

    public static void main(String[] args) {
        int[][] a = {{3,2},{2,4}};
        Arrays.sort(a, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        System.out.println(Arrays.toString(a[0]));

        List<Integer> list =new ArrayList<>();
        list.add(1);
        list.add(2);
        List<List<Integer>> b = new ArrayList<>();
        b.add(list);
        System.out.println(Arrays.toString(b.toArray()));
        Arrays.asList();
    }

    //https://leetcode.cn/problems/merge-intervals/?envType=problem-list-v2&envId=array
//    合并区间
    public int[][] merge(int[][] intervals) {
        if(intervals == null || intervals.length < 2) {
            return intervals;
        }

        Arrays.sort(intervals,(o1,o2) -> o1[0] - o2[0]); // 排序左区间

        List<int[]> ret = new ArrayList<>();
        ret.add(new int[]{intervals[0][0],intervals[0][1]});
        for(int i = 1;i < intervals.length;i++) {
            int l = intervals[i][0], r = intervals[i][1];
            if(l > ret.get(ret.size()-1)[1]) { // 分割区间了,添加新区间
                ret.add(new int[]{l,r});
            }else { // 有相同区间,进行合并
                ret.get(ret.size()-1)[1] = Math.max(ret.get(ret.size()-1)[1],r);
            }
        }



        return ret.toArray(new int[ret.size()][]);
    }

//    https://leetcode.cn/problems/contiguous-array/
//    连续数组
    public int findMaxLength(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();// <和,下标>
        map.put(0,-1);
        int sum = 0;
        int len = 0;
        for(int i = 0;i < nums.length;i++) {
            sum += nums[i] == 0 ? -1 : 1;

            if(map.containsKey(sum)) {
                len = Math.max(len,i - map.get(sum));
            }else {
                map.put(sum,i);
            }
        }

        return len;
    }
    // https://leetcode.cn/problems/jump-game/?envType=study-plan-v2&envId=top-interview-150
    // 跳跃游戏
    public boolean canJump(int[] nums) {
        int reach = 0;
        for (int i = 0;i < nums.length;i++) {
            if(i > reach) return false;

            reach = Math.max(reach,nums[i] + i);
        }
        return true;
    }
//    https://leetcode.cn/problems/jump-game-ii/submissions/678799301/?envType=study-plan-v2&envId=top-interview-150
    // 跳跃游戏二

    public int jump(int[] nums) {
        int step = 0;
        int position = nums.length - 1;
        while(position > 0) {
            for (int i = 0;i < position;i++) { // 找最左边能到position位置的表示最快
                if(nums[i] + i >= position) {
                    position = i;
                    step++;
                }
            }
        }
        return step;
    }
}
